/*
 * @Author: He Songda hesongda@haier.com
 * @Date: 2024-01-11 09:42:03
 * @LastEditors: He Songda hesongda@haier.com
 * @LastEditTime: 2024-01-11 17:20:45
 * @FilePath: \my-code-service\router\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 引入路由
const Router = require("koa-router");
const getHomeCode = require("../service/homeService/home.js");
const home = new Router();
const axios = require('axios')

home.get("/banner", async (ctx) => {
  console.log(getHomeCode.getHomeCode());
  ctx.body = "轮播";
});
home.get("/footer", async (ctx) => {
  ctx.body = "底部";
});
home.get("/weather/:city", async (ctx) => {
    try {  
      const city = ctx.params.city??beijing;
      // 调用第三方接口  
      const response = await axios.get(`https://api.seniverse.com/v3/weather/now.json?key=SOywoP340QRScdXkV&location=${city}&language=zh-Hans&unit=c`);  
  
      // 将第三方接口的数据返回给前端  
      ctx.body = response.data;  
    } catch (error) {  
      ctx.status = 500;  
      ctx.body = 'Error fetching data from third party API';  
      console.error(error);  
    }  
});

// 导出
module.exports = home;
